package org.big.usermanager.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.big.usermanager.dto.CreateUserRequest;
import org.big.usermanager.dto.UpdateUserRequest;
import org.big.usermanager.dto.UserResponse;

@Mapper
public interface UserMapper {

    @Select("SELECT " +
            "id, uuid, phone, email, nickname, real_name, avatar_url, gender, " +
            "birthday, status, status_reason, role_id, last_login_time, last_login_ip, " +
            "login_count, register_source, invite_code, invited_by, create_time, update_time " +
            "FROM user_account " +
            "WHERE id = #{userId} AND is_deleted = 0")
    UserResponse getUserById(Long userId);
        //todo  这里 优点问题
        // 2025-10-11 01:28:45 - JDBC Connection [HikariProxyConnection@1542946287 wrapping com.mysql.cj.jdbc.ConnectionImpl@2fecf1bb] will not be managed by Spring
        //2025-10-11 01:28:45 - ==>  Preparing: INSERT INTO user_account (uuid, phone, email, password_hash, salt, nickname, real_name, avatar_url, gender, birthday, status, role_id, permission_version, register_source, invite_code, invited_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        //2025-10-11 01:28:45 - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c1def23]
    @Insert("INSERT INTO user_account (" +
            "uuid, phone, email, password_hash, salt, nickname, real_name, " +
            "avatar_url, gender, birthday, status, role_id, permission_version, " +
            "register_source, invite_code, invited_by" +
            ") VALUES (" +
            "#{user.uuid}, #{user.phone}, #{user.email}, #{user.passwordHash}, #{user.salt}, " +
            "#{user.nickname}, #{user.realName}, #{user.avatarUrl}, #{user.gender}, " +
            "#{user.birthday}, #{user.status}, #{user.roleId}, #{user.permissionVersion}, " +
            "#{user.registerSource}, #{user.inviteCode}, #{user.invitedBy})")
    int insert(CreateUserRequest request);
    // todo 还没测试
    @Update("UPDATE user_account SET " +
            "nickname = #{user.nickname}, " +
            "email = #{user.email}, " +
            "real_name = #{user.realName}, " +
            "avatar_url = #{user.avatarUrl}, " +
            "gender = #{user.gender}, " +
            "birthday = #{user.birthday}, " +
            "update_time = NOW(), " +
            "version = version + 1 " +
            "WHERE id = #{userId} AND is_deleted = 0")
    int update(Long userId, UpdateUserRequest request);
}
